package util;

public class PaddingProcessor {
	private final static int BLOCK_SIZE = 8;

	public static byte[] addPadding(byte[] in, int blockSize) {
		int length = in.length; 
		if (length % blockSize == 0) {
			return in; 
		} else {
			byte[] out;
			int blockCount = length / blockSize;
			out = new byte[(blockCount+1) * blockSize];
			System.arraycopy(in, 0, out, 0, length);
			return out;
		}
		
	}

	public static byte[] removePadding(byte[] in, int blockSize) {
		int outLen = in.length;
		for (int i = BLOCK_SIZE + 1; i > 0&&outLen>0; i--) {

			if (in[--outLen] == (byte) 0x00) {
				 break;
			}
		}
		byte[] out = new byte[outLen];
		System.arraycopy(in, 0, out, 0, outLen);
		return out;
	}
}
